const app = getApp();
var tcity = require("../../utils/citys.js");
Page({
  data: {
    domainName: app.domainName,
    qiuName:'',
    qiuLei:['球队类型','5v5联赛球队','4V4球队','3V3球队'],
    qid:0,
    provinces: [],
    province: -1,
    citys: [],
    city: -1,
    countys: [],
    county: -1
  },
  setinput(e) {
    this.setData({qiuName:  e.detail.value});
  },
  onLoad(options) {
    var that = this;
    if(typeof(options.qid) != 'undefined'){
         wx.request({
          url: app.domainName + 'web/?c=getqname',
          method: 'POST',
          data: {id:options.qid}, header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: function (res) {
              that.setData({qiuName:res.data.n, qiuId: options.qid});
          }});
      console.log(options.qid)
    }
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    for(let i=0;i<cityData.length;i++){
      provinces.push(cityData[i].name);
    }
    that.setData({
      'provinces': provinces
    })
  },
  qiulei(e){
    this.setData({qid:e.detail.value});
  },
  setsheng(e){
    var _province=e.detail.value;
    var cityData = this.data.cityData;
    var _citys = [];
    for (let i = 0 ; i < cityData[_province].sub.length; i++) {
      _citys.push(cityData[_province].sub[i].name)
    }
    this.setData({province: _province, citys: _citys, city: -1, county: -1 });
    console.log(_citys);
  },
  setshi(e){
    var _city=e.detail.value;
    var _province=this.data.province;
    var cityData = this.data.cityData;
    var _countys=[];
    for (let i = 0 ; i < cityData[_province].sub[_city].sub.length; i++) {
      _countys.push(cityData[_province].sub[_city].sub[i].name)
    }
    this.setData({city: _city,countys:_countys, county: -1 });
  },
  setqu(e){
    this.setData({county: e.detail.value });
  },
  goZhuCe(){
      var _this=this;
      var _name=this.data.qiuName;
      var _qid=this.data.qid;
      var _sheng=this.data.province;
      var _shi=this.data.city;
      var _qu=this.data.county;
      if('' ==  _name){
        this.popMsg("请选择球队名称");
        return;
      }
      if('' ==  _qid || 0 ==  _qid){
        this.popMsg("请选择球队类型");
        return;
      }
      if(-1 ==  _sheng){
        this.popMsg("请选择省份");
        return;
      }
      if(-1 ==  _shi){
        this.popMsg("请选择市");
        return;
      }
      if(-1 ==  _qu){
        this.popMsg("请选择区");
        return;
      }

      var cityData = this.data.cityData;

      wx.request({
        url: app.domainName + 'web/?c=qiuzhu',
        method: 'POST',
        data: {openid:app.globalData.openid,name:_name,qtype:_qid,
          sheng:_sheng,shi:_shi,qu:_qu, shengname:cityData[_sheng].name,
          cityname:cityData[_sheng].sub[_shi].name,quname:cityData[_sheng].sub[_shi].sub[_qu].name
        }, header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          if('n' == res.data.s && 'name' == res.data.e){
            _this.popMsg("您填写的球队名已经存在");
            return;
          }
          wx.showToast({
            title: '创建成功',
            icon: 'success',
            duration: 1200
          })
          setTimeout(function(){
            wx.redirectTo({url: '/pages/qguan/index'})
          }, 1600);
          
        }});

  },
  popMsg(msg){
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 1500
    })
  }
 
})
